package com.itkaien.ssmconformitymaven.controller;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.itkaien.ssmconformitymaven.entiry.User;
import com.itkaien.ssmconformitymaven.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.function.ServerRequest;
import org.springframework.web.servlet.function.ServerResponse;

import java.util.List;

@Service
public class DefaultController {

    @Autowired
    private UserMapper userMapper;


    public ServerResponse getDefault(ServerRequest serverRequest) throws JsonProcessingException {
        List<User> users = userMapper.selectUsers();
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.enable(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES);

        String jsonString = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(users);
        objectMapper.reader();
        System.out.println(users);
        return ServerResponse.ok().body(jsonString);
    }

}
